#include <main.h>
#define DIALOG_MAPKEYS "Mapkeys"
#define DIALOG_MAPKEYS_RES "Mapkeys.res"

extern "C"
{
    typedef wfcStatus (*myCallback)();
}
class MycallbackClass : public virtual pfcUICommandActionListener
{
public:
    MycallbackClass(myCallback func) { m_func = func; }

    void OnCommand()
    {
        m_func();
    }

private:
    myCallback m_func;
};
class MyActionAccess : public virtual pfcUICommandAccessListener
{
public:
    MyActionAccess(pfcCommandAccess CommandAccess) { m_CommandAccess = CommandAccess; }
    pfcCommandAccess OnCommandAccess(xbool AllowErrorMessages)
    {
        return m_CommandAccess;
    }

private:
    pfcCommandAccess m_CommandAccess;
};

class MyDialogListener : public uifcDefaultDialogListener
{
public:
    void OnClose(uifcDialog_ptr handle)
    {
        uifcExitDialog(handle->GetDialog(), 0);
    }
};
class MyPushButtonListener : public uifcDefaultPushButtonListener
{
private:
    xstring ImageFile;

public:
    MyPushButtonListener(xstring filename)
    {
        ImageFile = filename;
    }
    void OnActivate(uifcPushButton_ptr handle)
    {
        xstring dialog = handle->GetDialog();
        uifcDialog_ptr CurDialog = uifcDialogFind(dialog, dialog);
        uifcLabel_ptr MyLable = uifcLabelFind(dialog, "lb_mapkeys");
        MyLable->SetImage(ImageFile);
        // CurDialog->SetTitleText(ImageFilename.Substring(0, strlen(ImageFilename) - 4));
        CurDialog->SetTitleText(handle->GetText());
    }
};
wfcStatus ShowMapkeys();

//程序入口
extern "C" int user_initialize(
    int argc,
    char *argv[],
    char *version[],
    char *build,
    wchar_t errbuf[80])
{
    pfcSession_ptr session = pfcGetProESession();
    try
    {
        pfcUICommand_ptr ShowMapkeys_com = CreateUICommand(session, "ShowMapkeys_com", new MycallbackClass(ShowMapkeys),
                                                           MSGFILE, "ShowMapkeys", "ShowMapkeys", "ShowMapkeys",
                                                           new MyActionAccess(pfcACCESS_AVAILABLE));
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }

    return 0;
}
//程序出口
extern "C" void user_terminate()
{
}

pfcUICommand_ptr CreateUICommand(pfcSession_ptr Session,
                                 xrstring Name,
                                 pfcUICommandActionListener_ptr Action,
                                 xrstring MessageFile,
                                 xrstring Label,
                                 optional xrstring Help,
                                 optional xrstring Description,
                                 pfcActionListener_ptr Listener,
                                 xrstring IconFile)
{
    pfcUICommand_ptr ret;
    ret = Session->UICreateCommand(Name, Action);
    ret->Designate(MessageFile, Label, Help, Description);
    ret->AddActionListener(Listener);
    if (IconFile != NULL)
        ret->SetIcon(IconFile);
    return ret;
}

wfcStatus ShowMapkeys()
{
    LOG(LOG_INFO) << "ShowMapkeys";
    uifcCreateDialog(DIALOG_MAPKEYS, DIALOG_MAPKEYS_RES);
    uifcDialog_ptr Dialog1 = uifcDialogFind(DIALOG_MAPKEYS, DIALOG_MAPKEYS);
    Dialog1->AddActionListener(new MyDialogListener());
    uifcLabel_ptr lable1 = uifcLabelFind(DIALOG_MAPKEYS, "lb_mapkeys");
    lable1->SetImage("mapkeys_X.jpg");
    lable1->SetWidth(54);
    xstring ImageFilename[] = {"mapkeys_X.jpg",
                               "mapkeys_W.jpg",
                               "mapkeys_Q.jpg",
                               "mapkeys_`.jpg",
                               "mapkeys_1.jpg",
                               "mapkeys_2.jpg",
                               "mapkeys_3.jpg",
                               "mapkeys_4.jpg",
                               "mapkeys_A.jpg",
                               "mapkeys_V.jpg",
                               "mapkeys_C.jpg",
                               "mapkeys_B.jpg",
                               "mapkeys_E.jpg",
                               "mapkeys_F.jpg",
                               "mapkeys_G.jpg",
                               "mapkeys_H.jpg",
                               "mapkeys_J.jpg",
                               "mapkeys_Z.jpg"};
    xstring ButtonName[] = {"btn_X",
                            "btn_W",
                            "btn_Q",
                            "btn_dot",
                            "btn_1",
                            "btn_2",
                            "btn_3",
                            "btn_4",
                            "btn_A",
                            "btn_V",
                            "btn_C",
                            "btn_B",
                            "btn_E",
                            "btn_F",
                            "btn_G",
                            "btn_H",
                            "btn_J",
                            "btn_Z"};
    int ImageNums = sizeof(ImageFilename) / sizeof(ImageFilename[0]);
    for (int i = 0; i < ImageNums; i++)
    {
        xstring Filename = ImageFilename[i];
        uifcPushButton_ptr btn = uifcPushButtonFind(DIALOG_MAPKEYS, ButtonName[i]);
        btn->AddActionListener(new MyPushButtonListener(Filename));
    }
    uifcActivateDialog(DIALOG_MAPKEYS);
    uifcDestroyDialog(DIALOG_MAPKEYS);
    return wfcTK_NO_ERROR;
}